<?php

/**
 * Project controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class ContentsController extends AppController {

	public function admin_index() {
		if (!empty($this->data)) {
			switch ($this->data['Paginate']['task']) {
				case 'add':
					$this->Dashboard->redirect(array('action' => 'add'));
			}
		}
		$contents = $this->Content->find('all', array(
			'order' => array('Content.created' => 'desc')));
		$this->set(compact('contents'));
	}

	public function admin_add() {
		if ($this->request->is('post')) {
			switch ($this->data['Paginate']['task']) {
				case 'back':
					$this->Dashboard->redirect(array('action' => 'index'), true, true);
				case 'save':
					if ($this->Content->save($this->data)) {
						$this->setFlash(__('Bài viết "%s" đã được lưu.', $this->data['Content']['title']), 'success');
						$this->Dashboard->redirect(array('action' => 'index'), true, true);
					} else {
						$this->setFlash(__('Đã có lỗi xảy ra, xin vui lòng thử lại'));
					}
			}
		}
		$this->set(compact('content'));
	}

	public function admin_edit($id = null) {
		$content = $this->Content->read(null, $id);
		$this->Dashboard->error(!$content);

		if ($this->request->is('post')) {
			$this->admin_add();
		} else {
			$this->data = $content;
		}
		$this->set(compact('content'));
	}

}
